package com.example.dangerouscargodriver.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.UserInfoController;
import com.example.dangerouscargodriver.entry.ResourceInfo;
import com.example.dangerouscargodriver.ui.activity.MyResourceDetailActivity;
import com.example.dangerouscargodriver.ui.activity.ResourceDetailActivity;

import java.util.ArrayList;
import java.util.List;

public class ResourceInfoAdapter extends RecyclerView.Adapter<ResourceInfoAdapter.ViewHolder>{

	private static final String TAG = "ResourceInfoAdapter";
	private List<ResourceInfo> mResourceInfoItemList = null;
	private Context mContext;
	private View mRootView;

	static class ViewHolder extends RecyclerView.ViewHolder{
		private TextView mFromName = null;
		private TextView mToName = null;
		private TextView mSourceDistance = null;
		private TextView mRegTime = null;
		private Context mContext = null;
		private LinearLayout mMetaLayout = null;
		private Button mActionGrabOrder = null;
		private ResourceInfo mResourceInfo = null;
		private LinearLayout mLayout = null;
		private ImageView mLevelView = null;
		private TextView mLoadTime = null;
		private TextView mExpectedPrice = null;
		private LinearLayout mPayOnline = null;
		private LinearLayout mShowPrice = null;

		public ViewHolder(View view) {
			super(view);
			mFromName = view.findViewById(R.id.item_start_city);
			mSourceDistance = view.findViewById(R.id.item_source_distance);
			mToName = view.findViewById(R.id.item_end_city);
			mRegTime = view.findViewById(R.id.item_register_time);
			mActionGrabOrder = view.findViewById(R.id.btn_grab_order);
			mMetaLayout = view.findViewById(R.id.layout_resource_meta);
			mLevelView = view.findViewById(R.id.iv_dangerous_level);
			mLoadTime = view.findViewById(R.id.tv_load_time);
			mShowPrice = view.findViewById(R.id.layout_show_price);
			mPayOnline = view.findViewById(R.id.layout_pay_online);
			mExpectedPrice = view.findViewById(R.id.tv_expected_price);
			mLayout = view.findViewById(R.id.layout_Resource_info_item);
		}
	}

	public ResourceInfoAdapter() {
		mResourceInfoItemList = new ArrayList<>();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		mRootView = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.layout_resource_info_item, parent, false);
		ViewHolder holder = new ViewHolder(mRootView);

		return holder;
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		final int selectPos = position;
		ResourceInfo info = mResourceInfoItemList.get(position);
		//执行绑定数据的操作
		holder.mFromName.setText(info.getFrom_name());
		holder.mToName.setText(info.getTo_name());
		holder.mSourceDistance.setText(info.getSource_distance());
		holder.mRegTime.setText(info.getRegtime());
		if(info.getName() != null && !info.getName().equals("")){
			View layout = LayoutInflater.from(mRootView.getContext()).inflate(R.layout.layout_resource_meta_info_item, null);
			TextView textView = layout.findViewById(R.id.tv_item_meta_string);
			textView.setText(info.getName());
			holder.mMetaLayout.addView(layout);
		}
		if(info.getClassname() != null && !info.getClassname().equals("")){
			View layout = LayoutInflater.from(mRootView.getContext()).inflate(R.layout.layout_resource_meta_info_item, null);
			TextView textView = layout.findViewById(R.id.tv_item_meta_string);
			textView.setText(info.getClassname());
			holder.mMetaLayout.addView(layout);
		}
		if(info.getWeight() != null && !info.getWeight().equals("")){
			View layout = LayoutInflater.from(mRootView.getContext()).inflate(R.layout.layout_resource_meta_info_item, null);
			TextView textView = layout.findViewById(R.id.tv_item_meta_string);
			textView.setText(info.getWeight() + "吨");
			holder.mMetaLayout.addView(layout);
		}
		if(info.getTruck_type() != null && !info.getTruck_type().equals("")){
			String truckType = info.getTruck_type();
			String[] truckTypeArray = truckType.split(",");

			int loopSize = truckTypeArray.length > 2? 2 : truckTypeArray.length;
			for(int index = 0; index < loopSize; index++){
				View layout = LayoutInflater.from(mRootView.getContext()).inflate(R.layout.layout_resource_meta_info_item, null);
				TextView textView = layout.findViewById(R.id.tv_item_meta_string);
				textView.setText(truckTypeArray[index]);
				if(index == 1 && truckTypeArray.length > 2){
					LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
					layoutParams.setMargins(0, 0, 0, 0);
					textView.setLayoutParams(layoutParams);
				}
				holder.mMetaLayout.addView(layout);
				if(index == 1 && truckTypeArray.length > 2){
					View layoutMore = LayoutInflater.from(mRootView.getContext()).inflate(R.layout.layout_resource_meta_info_item, null);
					TextView textViewMore = layoutMore.findViewById(R.id.tv_item_meta_string);
					textViewMore.setBackground(null);
					textViewMore.setText("……");
					holder.mMetaLayout.addView(layoutMore);
				}
			}
		}
		Glide.with(mRootView.getContext()).load(info.getIcon()).into(holder.mLevelView);

		if(info.getLoadtime2() != null){
			holder.mLoadTime.setText(info.getLoadtime() + "~" + info.getLoadtime2());
		}
		else {
			holder.mLoadTime.setText(info.getLoadtime());
		}
		holder.mExpectedPrice.setText(info.getExpected_price());
		if(info.getPay_type().equals("2")){
			holder.mPayOnline.setVisibility(View.VISIBLE);
		}
		else{
			holder.mPayOnline.setVisibility(View.GONE);
		}
		if(UserInfoController.Instance().getUserInfo() != null &&
				UserInfoController.Instance().getUserInfo().getRole() != 1){
			holder.mShowPrice.setVisibility(View.VISIBLE);
		}
		else{
			holder.mShowPrice.setVisibility(View.GONE);
		}
		holder.mActionGrabOrder.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mRootView.getContext(), ResourceDetailActivity.class);
				intent.putExtra("recid", mResourceInfoItemList.get(selectPos).getRecid());
				mRootView.getContext().startActivity(intent);
			}
		});

		holder.mLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mRootView.getContext(), ResourceDetailActivity.class);
				intent.putExtra("recid", mResourceInfoItemList.get(selectPos).getRecid());
				mRootView.getContext().startActivity(intent);
			}
		});
	}

	@Override
	public int getItemCount() {
		return mResourceInfoItemList.size();
	}

	public void addData(int position, ResourceInfo item) {
		mResourceInfoItemList.add(position, item);
		notifyItemInserted(position);
	}

	public void removeData(int position) {
		mResourceInfoItemList.remove(position);
		notifyItemRemoved(position);
	}

	public void updateData(int position, ResourceInfo item) {
		mResourceInfoItemList.set(position, item);
		notifyItemChanged(position);
	}

	public void clearData(){
		mResourceInfoItemList.clear();
		notifyDataSetChanged();
	}

	public void appendData(List<ResourceInfo> array) {
		for(int index = 0; index < array.size(); index++){
			mResourceInfoItemList.add(array.get(index));
		}
		notifyDataSetChanged();
	}
}
